<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>route | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="路由通过下述案例我们理解一下路由的基本概念，这是一个根据路径完成的类似状态匹配的分支处理概念
1234567891011121314151617181920212223242526272829303132333435ar http = require(&amp;#x27;http&amp;#x27;)var fs ">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="route"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>route</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/route/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T12:44:30.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="路由"><a href="#路由" class="headerlink" title="路由"></a>路由</h2><p>通过下述案例我们理解一下路由的基本概念，这是一个根据路径完成的类似状态匹配的分支处理概念</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">ar http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">var fs = require(<span class="string">&#x27;fs&#x27;</span>)</span><br><span class="line"></span><br><span class="line">http.createServer( <span class="keyword">function</span> ( req, res ) &#123;</span><br><span class="line"></span><br><span class="line">  switch ( req.url ) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;/home&#x27;</span>:</span><br><span class="line">      res.write(<span class="string">&#x27;home&#x27;</span>)</span><br><span class="line">      res.end()</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;/mine&#x27;</span>:</span><br><span class="line">      res.write(<span class="string">&#x27;mine&#x27;</span>)</span><br><span class="line">      res.end()</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;/login&#x27;</span>: </span><br><span class="line">      fs.readFile( <span class="string">&#x27;./static/login.html&#x27;</span>,<span class="keyword">function</span> ( error , data ) &#123;</span><br><span class="line">        <span class="keyword">if</span> ( error ) throw error  </span><br><span class="line">        res.write( data )</span><br><span class="line">        res.end()</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;/fulian.jpg&#x27;</span>:</span><br><span class="line">      fs.readFile( <span class="string">&#x27;./static/fulian.jpg&#x27;</span>, <span class="string">&#x27;binary&#x27;</span>, <span class="keyword">function</span>( error , data ) &#123;</span><br><span class="line">        <span class="keyword">if</span>( error ) throw error </span><br><span class="line">        res.write( data, <span class="string">&#x27;binary&#x27;</span> )</span><br><span class="line">        res.end()</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">    default: </span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line"> &#125;).listen( 8000, <span class="string">&#x27;localhost&#x27;</span>, <span class="function"><span class="title">function</span></span> () &#123;</span><br><span class="line">   console.log( <span class="string">&#x27;服务器运行在： http://localhost:8000&#x27;</span> )</span><br><span class="line"> &#125;)</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>